﻿using BrnMall.Core;
using BrnMall.Core.Domain.Staff;
using BrnMall.Manager.Domain.AddressBook;
using BrnMall.Manager.Manager.AddressBook;
using BrnMall.Services;
using QIDong.WeApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeiTap.Restful;

namespace QIDong.WeApp.Controllers
{
    /// <summary>
    /// 微街客-员工控制器
    /// </summary>
    public class StaffController : WebApi
    {

        #region 员工标签

        /// <summary>
        /// 员工标签列表
        /// </summary>
        /// <param name="staffId"></param>
        public List<StaffTag> StaffTagList(int staffId)
        {
            return StaffTagManager.GetList(staffId);
        }

        public object GetStaffTagList2(int staffId, int page = 1, int pageSize = 10)
        {
            var PageModel = new PageModel(pageSize, page, 10);
            var model = new
            {
                List = StaffTagManager.GetList(staffId),
                PageModel = PageModel,
            };
            return model;
        }

        /// <summary>
        /// 添加员工标签
        /// </summary>
        /// <returns></returns>
        public bool AddStaffTag(int staffId, string tagName)
        {
            if (StaffTagManager.Exists(staffId, tagName))
                throw new Exception("标签名重复");
            var List = StaffTagManager.GetList(staffId);
            if (List.Count>=10)
            {
                throw new Exception("标签的最大个数为10个。");
            }
            else { return StaffTagManager.Add(new StaffTag() { StaffId = staffId, TagName = tagName, UpCount = 0 }); }
        }

        /// <summary>
        /// 删除员工标签
        /// </summary>
        /// <param name="id"></param>
        /// <param name="staffId"></param>
        /// <returns></returns>
        public bool DeleteStaffTag(int id, int staffId = 0)
        {
            return StaffTagManager.Delete(id);
        }

        #endregion

        #region 部门员工

        /// <summary>
        /// 获取员工信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StaffModel GetInfo(int id)
        {
            var info = StaffManager.Get(id);
            if (info == null)
                throw new Exception("部门成员不存在");

            var stafTag = StaffTagManager.GetList(id);
            var region = info.Regionid > 0 ? Regions.GetRegionById(info.Regionid) : new BrnMall.Core.RegionInfo();

            var model = new StaffModel()
            {
                Id = info.Id,
                StoreId = info.StoreId,
                Uid = info.Uid,
                Avater = info.Avater,
                Name = info.Name,
                Company = info.Company,
                Phone = info.Phone,
                Email = info.Email,
                Mobile = info.Mobile,
                Address = info.Address,
                AiState = info.AiState,
                MobileState = info.MobileState,
                BossState = info.BossState,
                Position = info.Position,
                Regionid = info.Regionid,
                Department = info.Department,
                DepartmentId = info.DepartmentId,
                Sign = info.Sign,
                Addtime = info.Addtime,
                Wxid = info.Wxid,
                wxMobile = info.wxMobile,
                wxUserId = info.wxUserId,
                QrCode = info.QrCode,
                ImgList = new List<string>(),
                TagList = stafTag,
                ProvinceName = region.ProvinceName,
                CityName = region.CityName,
                AreaName = region.Name
            };

            return model;
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <returns></returns>
        public bool UpdateStaff(StaffModel model, string ProvinceName = "", string CityName = "", string AreaName = "")
        {
            if (string.IsNullOrEmpty(model.Avater))
                throw new Exception("请上传头像");

            if (string.IsNullOrEmpty(model.Mobile))
                throw new Exception("请填写手机号");
            if (string.IsNullOrEmpty(model.Phone))
                throw new Exception("请填写电话");
            if (string.IsNullOrEmpty(model.Email))
                throw new Exception("请填写邮箱");

            RegionInfo region = null;
            if (!string.IsNullOrEmpty(AreaName))
                region = Regions.GetRegionByNameAndLayer(AreaName, CityName, 3);
            else if (!string.IsNullOrEmpty(CityName))
                region = Regions.GetRegionByNameAndLayer(CityName, 3);
            else if (!string.IsNullOrEmpty(ProvinceName))
                region = Regions.GetRegionByNameAndLayer(AreaName, 1);

            if (region == null)
                throw new Exception("您的地区无效");

            model.Regionid = region.RegionId;

            if (model.Regionid > 0 && string.IsNullOrEmpty(model.Address))
                throw new Exception("请填写地址");


            return StaffManager.UpdateByStaff(model);
        }

        /// <summary>
        /// 修改个性签名
        /// </summary>
        /// <param name="staffId">员工ID</param>
        /// <param name="sign">签名内容</param>
        /// <returns></returns>
        public bool UpdateSign(int staffId, string sign)
        {
            if (staffId < 1)
                throw new Exception("请选择业务员");
            if (string.IsNullOrEmpty(sign))
                throw new Exception("请填写个性签名");

            return StaffManager.UpdpateSign(staffId, sign);
        }

        /// <summary>
        /// 修改成员手机号
        /// </summary>
        /// <returns></returns>
        public bool UpdateStaffMobile()
        {
            return false;
        }

        #endregion
        #region 部门管理

        public object GetMyDepartment(int staffId, int parentId = 0)
        {
            var staff = StaffManager.Get(staffId);
            var myDeptList = StaffDepartmentManager.GetMyDeportment(staffId);
            var deptList = DepartmentManager.GetList(staff.StoreId);

            var list = new List<object>();
            foreach (var dept in deptList)
            {
                foreach (var myDept in myDeptList)
                {
                    if (myDept.DepartmentId == dept.Id)
                    {
                        //if (dept.wxDepartmentId == parentId)
                        //{
                        list.Add(dept);
                        break;
                        //}
                    }
                }
            }
            return list;
        }

        #endregion

        #region 扩展设置
        /// <summary>
        /// 获取欢迎语
        /// </summary>
        /// <param name="staffId"></param>
        /// <returns></returns>
        public object GetWelcomeTip(int staffId)
        {
            return StaffExtendManager.GetWelcomeTip(staffId, (int)StaffExtendState.WelcomeTip);
        }

        /// <summary>
        /// 设置欢迎语
        /// </summary>
        /// <param name="staffId">员工ID</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public object SetWelcomeTip(int staffId, string content)
        {
            if (staffId == 0)
                throw new Exception("缺少员工参数");
            var model = new StaffExtend
            {
                AddTime = DateTime.Now,
                Module = (int)StaffExtendModule.WelcomeTip,
                Keyword = (int)StaffExtendState.WelcomeTip,
                Title = "设置欢迎语",
                Value = content,
                StaffId = staffId
            };
            var old = StaffExtendManager.GetWelcomeTip(model.StaffId, model.Keyword);
            if (old != null)
                return StaffExtendManager.Update(old.Id, model.Value);
            else
                return StaffExtendManager.Insert(model);
        }

        /// <summary>
        /// 获取自定义的跟进语
        /// </summary>
        /// <param name="staffId">员工ID</param>
        /// <returns></returns>
        public object GetFollowTip(int staffId)
        {
            return StaffExtendManager.GetWelcomeTipList(staffId, (int)StaffExtendState.FollowTip);
        }
        /// <summary>
        /// 添加自定义的跟进语
        /// </summary>
        /// <param name="staffId">员工ID</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public object AddFollowTip(int staffId, string content)
        {
            if (staffId == 0)
                throw new Exception("缺少员工参数");
            var model = new StaffExtend
            {
                AddTime = DateTime.Now,
                Module = (int)StaffExtendModule.FollowTip,
                Keyword = (int)StaffExtendState.FollowTip,
                Title = "设置跟进语",
                Value = content,
                StaffId = staffId
            };

            return StaffExtendManager.Insert(model);
        }
        /// <summary>
        /// 删除自定义的欢迎语
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteFollowTip(int id)
        {
           var res=StaffExtendManager.delete(id);
            if (res > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region  AI数据
        public object MyReport(int staffId)
        {
            /*
             
--客户总数
select top 10 * from crm_client where staffid=1
--跟进总数
select top 10 * from crm_ClientFollow where staffid=1
--浏览总数
select * from crm_ActionRecord where parentUid=1007
--被转发总数
select * from crm_ActionRecord where actionType=3 and parentUid=1007
--被保存总数
select * from crm_ActionRecord where actionType=7 and parentUid=1007
--被点赞数
select upCount from crm_staff where id=1


             */
            return null;
        }
        #endregion

    }
}